#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/9/10 9:30
# @Author : lenovo
# @Email : 26541796406@qq.com
# @File : CRM_ST_BG_463.py
# @Project : crm
"""添加公告，正确输入信息"""
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
"""选择Chrome浏览器，将driverAlert化，打开crm网页"""
driver = webdriver.Chrome()
alert = Alert(driver)
driver.get('http://192.168.1.139/crm3')
sleep(2)
"""找到用户名，密码输入框以及登录按钮"""
username_locator = driver.find_element_by_name('name')
password_locator = driver.find_element_by_name('password')
login_locator = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/form/fieldset/input[3]")
"""输入用户名，密码，点击登录"""
username_locator.send_keys('admin')
password_locator.send_keys('123456')
login_locator.click()
sleep(3)
"""
 这是在获取cookies值
 以后就可以免登录，直接绕过登录页面
cookies = driver.get_cookies()
cookie = {}
for k,v in cookies[0].items():
    if k == 'name' or k == 'value':
        cookie[k] = v
print(cookie)
"""
cookie = {'name': 'PHPSESSID', 'value': 'i1mj8tbl7cp4pt11rijh3toul0'}
driver.get('http://192.168.1.139')
sleep(1)
driver.maximize_window()
driver.add_cookie(cookie)
"""直接获得添加公告页面网址"""
driver.get('http://192.168.1.139/crm3/index.php?m=announcement&a=add')
sleep(2)
"""进行添加的操作"""
# 创建定位器
notice_title_locator = (By.CSS_SELECTOR,'input[name="title"]')  # 公告标题
notice_color_locator = (By.CSS_SELECTOR,'input[name="color"]')  # 公告标题颜色
notice_content_locator = (By.XPATH,'/html/body')  # 公告内容
notice_submit_locator = (By.NAME,'submit')  # 公告保存
# 具体操作
driver.find_element(*notice_title_locator).send_keys("本月目标")
sleep(1)
driver.find_element(*notice_color_locator).send_keys(Keys.CONTROL,'a')
sleep(1)
driver.find_element(*notice_color_locator).send_keys("78C9FF")
sleep(2)
driver.switch_to.frame(driver.find_element_by_class_name('ke-edit-iframe'))
sleep(1)
driver.find_element(*notice_content_locator).send_keys("新版本即将发布")
sleep(1)
driver.switch_to.parent_frame()
sleep(1)
driver.find_element(*notice_submit_locator).click()

sleep(1)
driver.close()